using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace Voodoo.Sauce.Privacy.UI
{
	[RequireComponent(typeof(Canvas))]
	[RequireComponent(typeof(GraphicRaycaster))]
	public class SettingsScreen : MonoBehaviour
	{
		public class Parameters
		{
			public Color mainColor;

			public string title;

			public string intro;

			public bool advertisingValue;

			public string advertising;

			public IEnumerable<string> advertisingUrls;

			public bool analyticsValue;

			public string analytics;

			public IEnumerable<string> analyticsUrls;

			public UnityAction accessDataAction;

			public string accessDataText;

			public UnityAction deleteDataCallback;

			public string deleteDataText;

			public UnityAction<PrivacyUpdate> saveAction;

			public string saveText;

			public UnityAction closeCallback;

			public string closeText;
		}

		[SerializeField]
		private Text _titleText;

		[SerializeField]
		private Text _introText;

		[SerializeField]
		private SimpleCheckbox _advertisingCheckbox;

		[SerializeField]
		private Text _advertisingText;

		[SerializeField]
		private Transform _advertisingContent;

		[SerializeField]
		private SimpleCheckbox _analyticsCheckbox;

		[SerializeField]
		private Text _analyticsText;

		[SerializeField]
		private Transform _analyticsContent;

		[SerializeField]
		private Button _accessDataButton;

		[SerializeField]
		private Text _accessDataButtonText;

		[SerializeField]
		private Button _deleteDataButton;

		[SerializeField]
		private Text _deleteDataButtonText;

		[SerializeField]
		private Button _saveButton;

		[SerializeField]
		private Text _saveButtonText;

		[SerializeField]
		private Image _saveButtonImage;

		[SerializeField]
		private Button _closeButton;

		[SerializeField]
		private Text _closeButtonText;

		[SerializeField]
		private Image[] _scrollbarHandles;

		[SerializeField]
		private PrivacyPolicyElement _privacyPolicyElementPrefab;

		private UnityAction<PrivacyUpdate> _saveAction;

		public void Initialize(Parameters p)
		{
			_titleText.text = p.title;
			_introText.text = p.intro;
			_advertisingCheckbox.Initialize(p.advertisingValue, isLocked: false);
			_advertisingText.text = p.advertising;
			InstantiatePrivacyPolicyElements(_advertisingContent, p.advertisingUrls);
			_analyticsCheckbox.Initialize(p.analyticsValue, isLocked: false);
			_analyticsText.text = p.analytics;
			InstantiatePrivacyPolicyElements(_analyticsContent, p.analyticsUrls);
			_accessDataButton.onClick.AddListener(p.accessDataAction);
			_accessDataButtonText.text = p.accessDataText;
			_deleteDataButton.onClick.AddListener(p.deleteDataCallback);
			_deleteDataButtonText.text = p.deleteDataText;
			_saveAction = p.saveAction;
			_saveButton.onClick.AddListener(SavePrivacy);
			_saveButtonText.text = p.saveText;
			_saveButtonImage.color = p.mainColor;
			_closeButton.onClick.AddListener(p.closeCallback);
			_closeButtonText.text = p.closeText;
			_closeButtonText.color = p.mainColor;
			Image[] scrollbarHandles = _scrollbarHandles;
			for (int i = 0; i < scrollbarHandles.Length; i++)
			{
				scrollbarHandles[i].color = p.mainColor;
			}
		}

		private void SavePrivacy()
		{
			PrivacyUpdate arg = new PrivacyUpdate
			{
				adsConsent = _advertisingCheckbox.IsChecked(),
				analyticsConsent = _analyticsCheckbox.IsChecked()
			};
			_saveAction(arg);
		}

		private void InstantiatePrivacyPolicyElements(Transform content, IEnumerable<string> texts)
		{
			int num = texts.Count();
			if (content.childCount > num)
			{
				for (int i = num; i < content.childCount; i++)
				{
					Object.Destroy(content.GetChild(i));
				}
			}
			int num2 = 0;
			foreach (string text in texts)
			{
				((num2 < content.childCount) ? content.GetChild(num2).GetComponent<PrivacyPolicyElement>() : Object.Instantiate(_privacyPolicyElementPrefab, content)).Initialize(new PrivacyPolicyElement.ConsentLineParameters
				{
					text = text,
					buttonAction = delegate
					{
						Application.OpenURL(text);
					}
				});
				num2++;
			}
		}
	}
}
